﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Telerik.Web.UI;
using Acms.AcmsControls;

public partial class UserControls_LoggedIn : UserControlBase
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        lnkLogoff.Click += new EventHandler(lnkLogoff_Click);
        
    }


    void mainMenu_ItemDataBound(object sender, Telerik.Web.UI.RadPanelBarEventArgs e)
    {
        var post = e.Item.DataItem as Acms.Bll.Post;
        if (post != null && CarbonExchangePage.AcmsContext.CurrentPost != null)
        {
            if (CarbonExchangePage.AcmsContext.CurrentPost.Id == post.Id)
            {
                e.Item.Expanded = true;
                e.Item.Selected = true;
            }
            else if (CarbonExchangePage.AcmsContext.CurrentPost.Path.StartsWith(post.Path))
            {
                e.Item.Expanded = true;
                e.Item.Selected = false;
            }
            else
            {
                e.Item.Expanded = false;
                e.Item.Selected = false;
            }
        }
        else if (post != null)
        {
            //devoping template
            e.Item.Expanded = true;
            e.Item.Selected = post.Sequence == 1;
        }
    }

    void lnkLogoff_Click(object sender, EventArgs e)
    {
        System.Web.Security.FormsAuthentication.SignOut();
        Response.Redirect(CarbonExchangePage.AcmsContext.OriginalUrl);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //check if user has audit
            if (Request.IsAuthenticated)
            {
                CarbonExchange.Bll.AuditFactory factory = new CarbonExchange.Bll.AuditFactory();
                var current = factory.GetCurrentForUserId(CarbonExchangePage.User.Identity.User.Id);
                lnkContinueAudit.Visible = !(lnkCreateAudit.Visible = current == null);
            }
        }
    }

    public override void DataBind()
    {
        BindMenu();
        imgThumb.FileId = CarbonExchangePage.User.Identity.User.GetThumbnailId();
        imgNoThumb.Visible = !imgThumb.FileId.HasValue;
        base.DataBind();
    }

    private void BindMenu()
    {
        if(CarbonExchangePage != null)
        {
            Acms.Bll.Menu menu = Acms.Bll.Menu.GetByNameAndSiteId("Main Menu", CarbonExchangePage.AcmsContext.SiteId);
            if (menu != null)
            {
                List<Post> posts = Post.GetAllBySiteIdMenuIdAndParentPath(CarbonExchangePage.AcmsContext.SiteId, menu.Id, "~/", CarbonExchangePage.AcmsContext.SiteVersion);
                BindPostChildren(mainMenu.Items, CarbonExchangePage.AcmsContext, posts, menu);
            }
            /*if(home != null)
            {
                mainMenu.Items.Add(BindMenuPost(CarbonExchangePage.AcmsContext, home));
                if (home.Id == CarbonExchangePage.AcmsContext.CurrentPost.Id)
                {
                    mainMenu.Items[0].Selected = true;
                }
                List<Post> tmp = Post.GetAllByMenuIdAndParentGuid(menu.Id, home.PostGuid, CarbonExchangePage.AcmsContext.SiteVersion);
                BindPostChildren(mainMenu.Items, CarbonExchangePage.AcmsContext, tmp, menu);
            }*/
        }

    }

    private void BindPostChildren(RadPanelItemCollection items, AcmsContext acmsContext, List<Post> children, Acms.Bll.Menu menu)
    {
        foreach (Post child in children)
        {
            RadPanelItem item = BindMenuPost(acmsContext, child);
            items.Add(item);
            bool expanded = false;

            if (acmsContext.CurrentPost != null)
            {
                if (acmsContext.CurrentPost.Id == child.Id)
                {
                    item.Selected = true;
                    expanded = true;
                    if (item.Parent != null)
                    {
                        item.Selected = false;
                    }
                }
                else if (acmsContext.CurrentPost.Path.StartsWith(child.Path) && child.Path != "~")
                {
                    expanded = true;
                    item.Selected = true;
                    if (item.Parent != null)
                    {
                        item.Selected = false;
                    }
                }
            }
            else
            {
                expanded = false;
            }

            if (expanded)
            {
                List<Post> tmp = Post.GetAllByMenuIdAndParentGuid(acmsContext.SiteId, menu.Id, child.PostGuid, acmsContext.SiteVersion);
                if (tmp != null && tmp.Count > 0)
                {
                    BindPostChildren(item.Items, acmsContext, tmp, menu);
                }
                else
                {
                    item.Selected = true;
                }
            }
        }
    }

    private RadPanelItem BindMenuPost(AcmsContext acmsContext, Post post)
    {
        RadPanelItem tmp = new RadPanelItem(post.DisplayName, post.Url);
        return tmp;
    }
}
